package com.jsb.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.security.Principal;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jsb.common.config.Global;
import com.jsb.common.utils.CookieUtils;
import com.jsb.common.utils.FileUtil;
import com.jsb.common.utils.IdGen;
import com.jsb.common.utils.PicUtil;
import com.jsb.model.Admin;
import com.jsb.model.User;
import com.jsb.shiro.UserUtils;

@Controller
public class IndexController {
    private static Logger alogger = Logger.getLogger(IndexController.class);

    @RequestMapping("/")
    public String index() {
        return "/login";
    }
    
    @RequestMapping("/redirect")
    public String toRegister(String url) {
        return "redirect:" + url;
    }


    public Color getRandColor(int fc, int bc) {
        Random rand = new Random();
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + rand.nextInt(bc - fc);
        int g = fc + rand.nextInt(bc - fc);
        int b = fc + rand.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    @RequestMapping("/randCode")
    public void randCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 首先设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        // 定义图片的宽度和高度
        int width = 90, height = 40;
        // 创建一个图像对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 得到图像的环境对象
        Graphics g = image.createGraphics();

        Random random = new Random();
        // 用随机颜色填充图像背景
        g.setColor(getRandColor(180, 250));
        g.fillRect(0, 0, width, height);
        for (int i = 0; i < 5; i++) {
            g.setColor(getRandColor(50, 100));
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            g.drawOval(x, y, 4, 4);
        }
        // 设置字体，下面准备画随机数
        g.setFont(new Font("", Font.PLAIN, 40));
        // 随机数字符串
        String sRand = "";
        for (int i = 0; i < 4; i++) {
            // 生成四个数字字符
            String rand = String.valueOf(random.nextInt(10));
            sRand += rand;
            // 生成随机颜色
            g.setColor(new Color(20 + random.nextInt(80), 20 + random.nextInt(100), 20 + random.nextInt(90)));
            // 将随机数字画在图像上
            g.drawString(rand, (17 + random.nextInt(3)) * i + 8, 34);

            // 生成干扰线
            for (int k = 0; k < 12; k++) {
                int x = random.nextInt(width);
                int y = random.nextInt(height);
                int xl = random.nextInt(9);
                int yl = random.nextInt(9);
                g.drawLine(x, y, x + xl, y + yl);
            }
        }

        // 将生成的随机数字字符串写入Session
        request.getSession().setAttribute("randCode", sRand);
        // 使图像生效
        g.dispose();
        // 输出图像到页面
        ImageIO.write(image, "JPEG", response.getOutputStream());
    }

    // 获取文件上传到服务器端的真是路径
    private String getFileRealPath(HttpServletRequest request) {
        String filePath = request.getSession().getServletContext().getRealPath("/");
        alogger.info("文件上传到服务器端的真是路径:" + filePath);
        return filePath;
    }
    
    @RequestMapping("/getPic/{pictype}/{url:.+}")
	public void getPhoto(@PathVariable(value = "url") String url,
						@PathVariable String pictype,
						HttpServletResponse response,HttpServletRequest request){
    	String path = "";
    	if(pictype.equals(Global.getConfig("pic.head"))){
    		path = PicUtil.getPicUploadPath() + Global.getConfig("pic.head");
    	}else if(pictype.equals(Global.getConfig("pic.cert"))){
    		path =PicUtil.getPicUploadPath() + Global.getConfig("pic.cert");
    	}else if(pictype.equals("article")){
    		path =request.getSession().getServletContext().getRealPath("/") + Global.getConfig("pic.article");
    	}else if(pictype.equals("question")){
    		path = PicUtil.getPicUploadPath() + Global.getConfig("pic.question");
    	}else if(pictype.equals("rotation")){
    		path = PicUtil.getPicUploadPath() + Global.getConfig("pic.rotation");
    	}else if(pictype.equals("cover")){
    		path = PicUtil.getPicUploadPath() + "cover";
    	}else if(pictype.equals("teacherHead")){
    		path = PicUtil.getPicUploadPath() + "teacherHead";
    	}else if(pictype.equals("wishcover")){
    		path = PicUtil.getPicUploadPath() + Global.getConfig("pic.wishcover");
    	}
		PicUtil.getPhoto(url, path, response);
	}
    /**
     * 上传图片
     * @param file
     * @param type
     * @param request
     */
    @RequestMapping("/uploadimage/{type}")
    @ResponseBody
    public String uploadimage(MultipartFile file,@PathVariable String type ,HttpServletRequest request){
    	File upFile = FileUtil.multipartToFile(file);
    	String fileName = IdGen.uuid();
    	String filePath = null;
    	if(type.equals("drama")){
    		filePath = PicUtil.getPicUploadPath() + Global.getConfig("pic.drama");
    	}else if(type.equals("rotation")){
    		filePath = PicUtil.getPicUploadPath() + Global.getConfig("pic.rotation");
    	}else if(type.equals("cover")){
    		filePath = PicUtil.getPicUploadPath() + "cover";
    	}else if(type.equals("teacherHead")){
    		filePath = PicUtil.getPicUploadPath() + "teacherHead";
    	}else if(type.equals("wishcover")){
    		filePath = PicUtil.getPicUploadPath() + Global.getConfig("pic.wishcover");
    	}
    	FileUtil.upFile(upFile, fileName, filePath);
    	return fileName;
    }
    
}
